package com.ego.item.controller;

import com.ego.common.pojo.PageResult;
import com.ego.item.entity.Sku;
import com.ego.item.entity.SpuBO;
import com.ego.item.entity.SpuDetail;
import com.ego.item.service.GoodsService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;

    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuBO>> page(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "page", defaultValue = "0") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows
    ) {
        return ResponseEntity.ok(goodsService.page(key, saleable, page, rows));
    }

    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBO spuBo) {
        goodsService.save(spuBo);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/spu/detail/{id}")
    public ResponseEntity<SpuDetail> findSpuDetailById(@PathVariable("id")Long id) {
        SpuDetail spuDetail = goodsService.findSpuDetailById(id);
        return ResponseEntity.ok(spuDetail);
    }

    @GetMapping("/skus")
    public ResponseEntity<List<Sku>>querySkuBySpuId(@RequestParam("spuId")Long spuId){
        List<Sku> skus = this.goodsService.querySkuBySpuId(spuId);
        return ResponseEntity.ok(skus);
    }

    @PutMapping
    public ResponseEntity<Void> updateGoods(@RequestBody SpuBO spuBo) {
        goodsService.update(spuBo);
        return ResponseEntity.ok().build();
    }
    /**
     * 逻辑删除商品
     * @param id
     * @return
     */
    @DeleteMapping
    public ResponseEntity<Void> delete(@RequestParam("id") Long id) {
        goodsService.deleteSpuById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PutMapping("/shelf")
    public ResponseEntity<Void> shelf(@RequestParam("id") Long id){
        goodsService.updateShelf(id);
        return ResponseEntity.ok().build();
    }

    /*门户分页*/
    @GetMapping("/spu/pageForEs")
    public ResponseEntity<PageResult<SpuBO>> page(
            @RequestParam(value = "saleable",required = false)Boolean saleable,
            @RequestParam("page")Integer page,
            @RequestParam("rows")Integer rows
    ){
        return ResponseEntity.ok(goodsService.goodsPage(saleable, page, rows));
    }


    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("/spubo/{id}")
    public ResponseEntity<SpuBO> queryGoodsById(@PathVariable("id") Long id){
        SpuBO spuBo=this.goodsService.queryGoodsById(id);
        if (spuBo == null){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(spuBo);
    }

    @GetMapping("/sku/{skuId}")
    public Sku querySkuBySkuId(@PathVariable("skuId") Long skuId){
        return goodsService.findSkuBySkuId(skuId);
    }

    @GetMapping("/skusById")
    public List<Sku> querySkuListBySkuIds(@RequestParam("skuIdList") List<Long> skuIdList){
        return goodsService.findSkuListBySkuIds(skuIdList);
    }

    @PostMapping("/reduceStock")
    public Boolean reduceStock(@RequestBody List<Map<Object, Object>> orderDetailList) {
        return goodsService.reduceStock(orderDetailList);
    }

}
